<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class ArticleCategory extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'article_category';

    protected $primaryKey = 'cate_id';

    public function article()
    {
        return $this->hasMany(Article::class, 'cate_id', 'cate_id');
    }

    public function getList($where = [], $with = [], $select = ['*'], $limit = 0)
    {
        $data = $this->newQuery()
            ->where($where)
            ->when($with, function ($query, $with) {
                return $query->with($with);
            })
            ->select($select)
            ->orderBy('sort', 'asc')
            ->when($limit, function ($query, $limit) {
                return $query->limit($limit);
            })
            ->get();

            return $data ? $data->toArray() : [];
    }

    public function getRow($id, $with = [], $select = ['*'])
    {
        $data =  $this->newQuery()
            ->where(['cate_id' => $id])
            ->when($with, function ($query, $with) {
                return $query->with($with);
            })
            ->select($select)
            ->first();

        return $data ? $data->toArray() : (object)[];
    }

}
